package com.ruoyi.personnel.oa.attendance.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.personnel.management.staff.domain.Staff;
import com.ruoyi.personnel.management.staff.mapper.StaffMapper;
import com.ruoyi.personnel.oa.attendance.domain.Attendance;
import com.ruoyi.personnel.oa.attendance.domain.AttendanceCond;
import com.ruoyi.personnel.oa.attendance.domain.StaffAttendance;
import com.ruoyi.personnel.oa.attendance.mapper.AttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 考勤
 * 业务逻辑
 */
@Service
public class AttendanceServiceImpl implements IAttendanceService{
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private StaffMapper staffMapper;

    /**
     * 根据搜索条件所有考勤记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<Attendance> selectAttendanceList(AttendanceCond cond) {
        return attendanceMapper.selectAttendanceList(cond);
    }

    /**
     * 查看所有员工的打卡状态
     * @param staff
     * @return
     */
    @Override
    public List<StaffAttendance> selectStaffAttendanceList(Staff staff) {
        List<Staff> staffList = staffMapper.selectStaffList(staff);
        List<StaffAttendance> resultList = new ArrayList<>();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String formatDate = sdf.format(date);
        for (Staff item : staffList) {
            StaffAttendance staffAttendance = new StaffAttendance();
            staffAttendance.setStaffId(item.getStaffId());
            staffAttendance.setStaffName(item.getStaffName());
            staffAttendance.setDeptName(item.getDepartmentName());
            staffAttendance.setPostName(item.getPositionInfoName());
            if (StringUtils.isNotNull(attendanceMapper.selectAttendanceById(item.getStaffId(), formatDate))) {
                // 打卡
                staffAttendance.setAttendanceFlag(ERPConstant.COMMON_YES); //1
            } else {
                staffAttendance.setAttendanceFlag(ERPConstant.COMMON_NO); //0
            }
            resultList.add(staffAttendance);
        }
        return resultList;
    }

    /**
     * 根据编号搜索具体的考勤记录
     * @param staffId 编号
     * @return
     */
    @Override
    public boolean selectAttendanceById(String staffId, String clockTime) {
        return StringUtils.isNotNull(attendanceMapper.selectAttendanceById(staffId, clockTime));
    }

    /**
     * 新增考勤
     * @param attendance
     * @return
     */
    @Override
    public int insertAttendance(Attendance attendance) {
        return attendanceMapper.insertAttendance(attendance);
    }

    /**
     * 删除考勤
     * 仅修改删除标志
     * @param attendance
     * @return
     */
    @Override
    public int updateAttendanceDeleteFlag(Attendance attendance) {
        return attendanceMapper.updateAttendanceDeleteFlag(attendance);
    }
}
